/**
 * @author 铭
 * @TODO 10/1/2024
 */
package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbHospitalDao;
import com.woniuxy.daos.TbHospitalManagerDao;
import com.woniuxy.entitys.TbHospital;
import com.woniuxy.entitys.TbHospitalExample;
import com.woniuxy.entitys.TbHospitalManager;
import com.woniuxy.entitys.TbHospitalManagerExample;
import com.woniuxy.service.HospitalInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class HospitalInfoServiceImpl implements HospitalInfoService {
    @Resource
    private TbHospitalDao tbHospitalDao;
    @Resource
    private TbHospitalManagerDao tbHospitalManagerDao;
    @Override
    public PageInfo search(Integer pageNum, List<String> list, String input) {
        PageHelper.startPage(pageNum,10);
        List<TbHospital> hospitals = null;
        input = "%" + input + "%";
        if (list!=null && list.size()!=0){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            String star = null;
            String end = null;
            try {
                star = simpleDateFormat.format(simpleDateFormat.parse(list.get(0)));
                end = simpleDateFormat.format(simpleDateFormat.parse(list.get(1)));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            hospitals = tbHospitalDao.search(star,end,input);
        }else {
            hospitals = tbHospitalDao.search(null,null,input);

        }
        PageInfo pageInfo = new PageInfo(hospitals);
        return pageInfo;
    }

    @Override
    public int refDel(Integer id) {
        TbHospital tbHospital = new TbHospital();
        tbHospital.setId(id);
        tbHospital.setSoftdel(0);
        tbHospital.setBeiyong1("0");
        TbHospital tbHospital1 = tbHospitalDao.selectByPrimaryKey(id);
        Integer mid = Integer.valueOf(tbHospital1.getBeiyong1());
        TbHospitalManager tbHospitalManager = tbHospitalManagerDao.selectByPrimaryKey(mid);
        if (tbHospitalManager!=null){
            tbHospitalManager.setHid(0);
            tbHospitalManagerDao.updateByPrimaryKeySelective(tbHospitalManager);

        }


        return tbHospitalDao.updateByPrimaryKeySelective(tbHospital);
    }

    @Override
    public List<TbHospitalManager> selMans() {
        TbHospitalManagerExample tbHospitalManagerExample = new TbHospitalManagerExample();
        TbHospitalManagerExample.Criteria criteria = tbHospitalManagerExample.createCriteria();
        criteria.andHidEqualTo(0);
        return tbHospitalManagerDao.selectByExample(tbHospitalManagerExample);
    }

    @Override
    public int addHos(TbHospital hospital) {
        hospital.setSoftdel(1);
        hospital.setCreateTime(new Date());
        int insert = tbHospitalDao.insert(hospital);
        System.out.println(insert);
        if (insert>0){
            TbHospitalExample tbHospitalExample = new TbHospitalExample();
            TbHospitalExample.Criteria criteria = tbHospitalExample.createCriteria();
            criteria.andHnameEqualTo(hospital.getHname());
            criteria.andProvinceEqualTo(hospital.getProvince());
            criteria.andCityEqualTo(hospital.getCity());
            criteria.andDistrictEqualTo(hospital.getDistrict());
            criteria.andAccountEqualTo(hospital.getAccount());
            criteria.andBeiyong1EqualTo(hospital.getBeiyong1());
            List<TbHospital> hospitals = tbHospitalDao.selectByExample(tbHospitalExample);
            Integer id = hospitals.get(0).getId();
            Integer manId = Integer.parseInt(hospital.getBeiyong1());
            TbHospitalManager tbHospitalManager = new TbHospitalManager();
            tbHospitalManager.setId(manId);
            tbHospitalManager.setHid(id);
            return tbHospitalManagerDao.updateByPrimaryKeySelective(tbHospitalManager);
        }

        return 0;
    }

    @Override
    public int saveUpd(TbHospital hospital) {
        Integer id = hospital.getId();
        TbHospital old = tbHospitalDao.selectByPrimaryKey(id);
        Integer oldmid = Integer.valueOf(old.getBeiyong1());
        TbHospitalManager oldmanage = tbHospitalManagerDao.selectByPrimaryKey(oldmid);
        if (oldmanage!=null){
            oldmanage.setHid(0);
            int i = tbHospitalManagerDao.updateByPrimaryKeySelective(oldmanage);

        }
        int i1 = tbHospitalDao.updateByPrimaryKeySelective(hospital);
        Integer newManage = Integer.valueOf(hospital.getBeiyong1());
        TbHospitalManager tbHospitalManager = tbHospitalManagerDao.selectByPrimaryKey(newManage);
        tbHospitalManager.setHid(hospital.getId());
        int i = tbHospitalManagerDao.updateByPrimaryKeySelective(tbHospitalManager);

        return  i + i1;

    }

}
